package com.framework.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.dao.ter.TerFlowInfoDao;
import com.framework.entity.sys.SysAgent;
import com.framework.entity.ter.TerFlowInfo;
import com.framework.entity.ter.TerTerminalInfo;
import com.framework.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @author wq
 */
@Service("terFlowInfoService")
public class TerFlowInfoServiceImpl extends ServiceImpl<TerFlowInfoDao, TerFlowInfo> implements TerFlowInfoService {

    @Autowired
    private TerInfoDetailService terInfoDetailService;

    @Autowired
    private DistribuService distribuService;

    @Autowired
    private TerminalInfoService terminalInfoService;

    @Autowired
    private AgentService agentService;

    @Override
    public Long getInfoId(String sn, Long deptId) {
        TerFlowInfo t = new TerFlowInfo();
        t.setSn(sn);
        t.setDeptId(deptId);
        TerFlowInfo info = baseMapper.selectOne(t);
        if (info != null) {
            return info.getInfoId();
        } else {
            t.setCreateTime(new Date());
            baseMapper.insert(t);
            return selectOne(new EntityWrapper<TerFlowInfo>().eq("sn", sn).eq("dept_id", deptId)).getInfoId();
        }


    }

    @Override
    public void addDistribuInfo(String sn, Long deptId) {
        Long infoId = getInfoId(sn, deptId);

        Long agentId = distribuService.queryTerminalOwn(sn);

        SysAgent acceptAgent = agentService.queryAgentById(agentId);

        SysAgent baseAgent = agentService.queryBasicAgentBydeptId(deptId);

        TerTerminalInfo info = terminalInfoService.queryTerminalBySn(sn);

        SysAgent reapAgent = agentService.queryAgentById(info.getPurchaserId());
        String reapName;
        if (reapAgent == null) {
            reapName = "无";
        } else {
            reapName = reapAgent.getName();
        }
        terInfoDetailService.addDistribuDetail(infoId, 1, baseAgent.getName(), acceptAgent.getName(), info.getIsOnLine().intValue(), info.getReapAmount() == null ? new BigDecimal("0.00") : info.getReapAmount(), reapName);

    }

    @Override
    public void addReportInfo(String sn, SysAgent reportAgent) {
        Long infoId = getInfoId(sn, reportAgent.getDeptId());

        terInfoDetailService.addReportDetail(infoId, 2, reportAgent.getName(), reportAgent.getName());

    }

    @Override
    public void addDisReportInfo(String sn, Long deptId, SysAgent agent) {
        Long infoId = getInfoId(sn, deptId);


        terInfoDetailService.addDisReportDetail(infoId, 3, agent.getName());
    }

    @Override
    public void addVerifyTranInfo(String sn, Long deptId) {
        Long infoId = getInfoId(sn, deptId);
        terInfoDetailService.addNoParamDetail(infoId, 4);
    }


    @Override
    public void addActivationAndReapInfo(String sn, Long deptId) {
        Long infoId = getInfoId(sn, deptId);

        TerTerminalInfo info = terminalInfoService.queryTerminalBySn(sn);
        //线上活动机器
        if (info.getIsOnLine().intValue() == 1) {
            terInfoDetailService.addActivationDetail(infoId, info.getIsReport().intValue() == 2 ? 5 : 6,info.getReapAmount());
            //线下报备
        } else {
            terInfoDetailService.addNoParamDetail(infoId, info.getIsReport().intValue() == 2 ? 7 : 8);
        }

    }

    @Override
    public void addReportedInfo(String sn, Long deptId,TerTerminalInfo info) {
        Long infoId = getInfoId(sn, deptId);

        //线上活动机器
        if (info.getIsOnLine().intValue() == 1) {
            if (info.getIsActivation().intValue() == 2) {

                terInfoDetailService.addActivationDetail(infoId, 6, info.getReapAmount());
            } else {
                terInfoDetailService.addNoParamDetail(infoId, 9);
            }
        } else {
            if (info.getIsActivation().intValue() == 2) {

                terInfoDetailService.addNoParamDetail(infoId, 8);
            } else {
                terInfoDetailService.addNoParamDetail(infoId, 10);
            }
        }


    }
}
